<template>
  <div v-if="modalOpen" class="auth-overlay">
    <div class="auth-modal">
      <!-- 授权弹窗头部 -->
      <div class="auth-header" :class="provider">
        <div class="auth-logo">
          <component :is="getProviderIcon(provider)" class="provider-icon" />
        </div>
        <div class="auth-title">
          <h3>{{ getProviderName(provider) }}</h3>
          <p>授权登录</p>
        </div>
      </div>

      <!-- 授权内容 -->
      <div class="auth-content">
        <div class="app-info">
          <div class="app-logo">
            <img src="/src/assets/logo.svg" alt="智教易购" class="app-icon" />
          </div>
          <div class="app-details">
            <h4>智教易购</h4>
            <p>在线学习平台</p>
          </div>
        </div>

        <div class="permission-list">
          <div class="permission-item">
            <CheckCircleOutlined class="permission-icon" />
            <span>申请获取你的公开信息（昵称、头像等）</span>
          </div>
          <div class="permission-item">
            <CheckCircleOutlined class="permission-icon" />
            <span>获取你的基本资料</span>
          </div>
          <div class="permission-item">
            <CheckCircleOutlined class="permission-icon" />
            <span>获取你的登录状态</span>
          </div>
        </div>

        
      </div>

      <!-- 授权按钮 -->
      <div class="auth-actions">
        <a-button 
          type="primary" 
          size="large" 
          class="auth-btn agree-btn"
          :class="provider"
          @click="handleAgree"
        >
          同意
        </a-button>
        <a-button 
          size="large" 
          class="auth-btn cancel-btn"
          @click="handleCancel"
        >
          取消
        </a-button>
      </div>

      <!-- 用户类型选择（同意后显示） -->
      <div v-if="showUserTypeSelection" class="user-type-selection">
        <div class="selection-header">
          <div class="selection-icon">
            <UserOutlined />
          </div>
          <h4>选择用户类型</h4>
          <p>请选择要模拟的用户类型</p>
        </div>
        <div class="selection-options">
          <div class="selection-card" @click="handleUserTypeSelect('new')">
            <div class="card-icon new-user-icon">
              <UserAddOutlined />
            </div>
            <div class="card-content">
              <h5>新用户（首次登录）</h5>
              <p>模拟首次使用第三方登录的用户，需要完善账号信息</p>
            </div>
            <div class="card-arrow">
              <RightOutlined />
            </div>
          </div>
          
          <div class="selection-card" @click="handleUserTypeSelect('existing')">
            <div class="card-icon existing-user-icon">
              <UserOutlined />
            </div>
            <div class="card-content">
              <h5>老用户（已绑定）</h5>
              <p>模拟已绑定账号的用户，直接登录成功</p>
            </div>
            <div class="card-arrow">
              <RightOutlined />
            </div>
          </div>
        </div>
        
        
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import {
  WechatOutlined,
  VideoCameraOutlined,
  CheckCircleOutlined,
  UserOutlined,
  UserAddOutlined,
  RightOutlined
} from '@ant-design/icons-vue'

interface Props {
  open: boolean
  provider: string
  providerConfig: any
}

interface Emits {
  (e: 'update:open', value: boolean): void
  (e: 'auth-success', userType: 'new' | 'existing'): void
  (e: 'auth-cancel'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

// 计算属性用于v-model:open
const modalOpen = computed({
  get: () => props.open,
  set: (value: boolean) => {
    emit('update:open', value)
    // 当弹窗关闭时，重置用户类型选择状态
    if (!value) {
      showUserTypeSelection.value = false
    }
  }
})

// 用户类型选择状态
const showUserTypeSelection = ref(false)

// 获取第三方平台图标
const getProviderIcon = (provider: string) => {
  switch (provider) {
    case 'wechat':
      return WechatOutlined
    case 'douyin':
      return VideoCameraOutlined
    default:
      return WechatOutlined
  }
}

// 获取第三方平台名称
const getProviderName = (provider: string) => {
  switch (provider) {
    case 'wechat':
      return '微信'
    case 'douyin':
      return '抖音'
    default:
      return '第三方平台'
  }
}

// 处理同意授权
const handleAgree = () => {
  console.log('ThirdPartyAuthModal - 用户点击同意授权')
  showUserTypeSelection.value = true
}

// 处理取消授权
const handleCancel = () => {
  emit('auth-cancel')
}

// 处理用户类型选择
const handleUserTypeSelect = (userType: 'new' | 'existing') => {
  emit('auth-success', userType)
}

// 监听弹窗打开状态，确保每次打开时都重置状态
watch(() => props.open, (newOpen) => {
  console.log('ThirdPartyAuthModal - 弹窗状态变化:', newOpen)
  if (newOpen) {
    // 弹窗打开时，重置用户类型选择状态
    showUserTypeSelection.value = false
    console.log('ThirdPartyAuthModal - 重置用户类型选择状态')
  }
})
</script>

<style scoped>
.auth-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  backdrop-filter: blur(4px);
}

.auth-modal {
  background: white;
  border-radius: 16px;
  width: 400px;
  max-width: 90vw;
  max-height: 90vh;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: slideIn 0.3s ease-out;
  display: flex;
  flex-direction: column;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

.auth-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 24px;
  text-align: center;
  position: relative;
  flex-shrink: 0;
}

.auth-header.wechat {
  background: linear-gradient(135deg, #07c160 0%, #06ad56 100%);
}

.auth-header.douyin {
  background: linear-gradient(135deg, #000000 0%, #333333 100%);
}

.auth-logo {
  margin-bottom: 16px;
}

.provider-icon {
  font-size: 48px;
  color: white;
}

.auth-title h3 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 600;
}

.auth-title p {
  margin: 0;
  font-size: 16px;
  opacity: 0.9;
}

.auth-content {
  padding: 24px;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

.app-info {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  padding: 20px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 16px;
  border: 1px solid #e9ecef;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.app-logo {
  margin-right: 16px;
}

.app-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
}

.app-details h4 {
  margin: 0 0 4px 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.app-details p {
  margin: 0;
  font-size: 14px;
  color: #666;
}

.permission-list {
  margin-bottom: 20px;
}

.permission-item {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  padding: 16px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 12px;
  border: 1px solid #e9ecef;
  transition: all 0.3s ease;
}

.permission-item:hover {
  background: linear-gradient(135deg, #e6f7ff 0%, #bae7ff 100%);
  border-color: #91d5ff;
  transform: translateX(4px);
}

.permission-icon {
  color: #52c41a;
  font-size: 16px;
  margin-right: 12px;
}

.permission-item span {
  font-size: 14px;
  color: #333;
}



.auth-actions {
  padding: 0 24px 24px;
  display: flex;
  gap: 12px;
  flex-shrink: 0;
}

.auth-btn {
  flex: 1;
  height: 48px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.auth-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s ease;
}

.auth-btn:hover::before {
  left: 100%;
}

.agree-btn {
  background: #07c160;
  border-color: #07c160;
}

.agree-btn:hover {
  background: #06ad56;
  border-color: #06ad56;
}

.agree-btn.wechat {
  background: #07c160;
  border-color: #07c160;
}

.agree-btn.wechat:hover {
  background: #06ad56;
  border-color: #06ad56;
}

.agree-btn.douyin {
  background: #000000;
  border-color: #000000;
}

.agree-btn.douyin:hover {
  background: #333333;
  border-color: #333333;
}

.cancel-btn {
  border-color: #d9d9d9;
  color: #666;
}

.cancel-btn:hover {
  border-color: #1890ff;
  color: #1890ff;
}

.user-type-selection {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  animation: slideIn 0.3s ease-out;
  overflow: hidden;
}

.selection-header {
  text-align: center;
  margin-bottom: 32px;
  animation: fadeInUp 0.6s ease-out;
}

.selection-icon {
  margin-bottom: 16px;
}

.selection-icon .anticon {
  font-size: 48px;
  color: #1890ff;
}

.selection-header h4 {
  margin: 0 0 8px 0;
  font-size: 24px;
  color: #333;
  font-weight: 600;
}

.selection-header p {
  margin: 0;
  font-size: 16px;
  color: #666;
}

.selection-options {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
  max-width: 320px;
}

.selection-card {
  display: flex;
  align-items: center;
  padding: 20px;
  background: #f8f9fa;
  border: 2px solid transparent;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  animation: fadeInUp 0.6s ease-out;
}

.selection-card:nth-child(1) {
  animation-delay: 0.1s;
}

.selection-card:nth-child(2) {
  animation-delay: 0.2s;
}

.selection-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(24, 144, 255, 0.1) 0%, rgba(24, 144, 255, 0.05) 100%);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.selection-card:hover {
  border-color: #1890ff;
  background: #f0f8ff;
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(24, 144, 255, 0.15);
}

.selection-card:hover::before {
  opacity: 1;
}

.card-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  flex-shrink: 0;
  transition: all 0.3s ease;
}

.selection-card:hover .card-icon {
  animation: pulse 0.6s ease-in-out;
}

.new-user-icon {
  background: linear-gradient(135deg, #52c41a 0%, #73d13d 100%);
  color: white;
}

.new-user-icon .anticon {
  font-size: 24px;
}

.existing-user-icon {
  background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
  color: white;
}

.existing-user-icon .anticon {
  font-size: 24px;
}

.card-content {
  flex: 1;
}

.card-content h5 {
  margin: 0 0 4px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.card-content p {
  margin: 0;
  font-size: 14px;
  color: #666;
  line-height: 1.4;
}

.card-arrow {
  color: #d9d9d9;
  transition: all 0.3s ease;
}

.selection-card:hover .card-arrow {
  color: #1890ff;
  transform: translateX(4px);
}



/* 响应式设计 */
@media (max-width: 480px) {
  .auth-modal {
    width: 95vw;
    max-height: 90vh;
    margin: 20px;
  }
  
  .auth-header {
    padding: 12px;
    flex-shrink: 0;
  }
  
  .auth-content {
    padding: 12px;
    flex: 1;
    overflow: hidden;
  }
  
  .auth-actions {
    padding: 0 12px 12px;
    flex-shrink: 0;
  }
  
  .provider-icon {
    font-size: 32px;
  }
  
  .auth-title h3 {
    font-size: 16px;
  }
  
  .auth-title p {
    font-size: 12px;
  }
  
  .app-info {
    padding: 12px;
    margin-bottom: 16px;
  }
  
  .permission-item {
    padding: 8px;
    margin-bottom: 8px;
  }
  
  .permission-item span {
    font-size: 12px;
  }
  
  .selection-options {
    max-width: 100%;
  }
  
  .selection-card {
    padding: 12px;
  }
  
  .card-icon {
    width: 36px;
    height: 36px;
    margin-right: 10px;
  }
  
  .card-content h5 {
    font-size: 13px;
  }
  
  .card-content p {
    font-size: 11px;
  }
}
</style>
